package xyf.p02.stack_queue;

import java.util.Stack;

/**
 * ClassName: J031_StackPushPopSequece
 * Description:
 * Author: xyf
 * Date: 9/3/22 6:23 PM
 * Version: 1.0
 **/
public class J031_StackPushPopSequece
{
    private Stack<Integer> stack = new Stack<>();

    public boolean validateStackSequences(int[] pushed, int[] popped)
    {
        int popIndex = 0;
        for (int i = 0; i < pushed.length; i++)
        {
            stack.push(pushed[i]);

            while (!stack.isEmpty() && popIndex < popped.length && popped[popIndex] == stack.peek())
            {
                stack.pop();
                popIndex++;
            }
        }

        return stack.isEmpty();
    }
}